<template>
	<view class="purchase">
		<!-- <view class="header-top" :style="{height: top+'px'}">

		</view>
		<view class="header-line" :style="{height:height+'px'}">

			<view class="left" @click="shang">
				<van-icon name="arrow-left" size="17px" color="#ffffff" />
			</view>
			<view class="right" style="text-align: center;">
				<view class="search">
					确认下单
				</view>
			</view>

		</view>
		<view class="box" :style="{height:'10'+'px'}">

		</view> -->


		<view class="purchase_box">
			<view class="purchase_box_top">
				<view class="purchase_box_top_a">
					<view style="width: 710rpx;margin: 0 auto;">
						<image class="img" src="../../static/img/bian.png"></image>
						<view class="purchase_box_top_a_bottom">
							<view class="purchase_box_top_a_left" @click="determined">
								<image class="imgss" src="../../static/imgs/dizhi.png"></image>
								<view class="zhong1" v-if="display">
									请输入下单地址
								</view>
								<view class="zhong2" v-else>
									<view class="shang">
										<view class="shang_left">
											<text class="text1">{{addressInfo.realName}}</text>
											<!-- <image class="img" src="../../static/imgs/nv.png"></image> -->
										</view>
										<text class="text2" style="margin-left: 30rpx;">{{addressInfo.phone}}</text>
									</view>
									<view class="xia">
										{{addressInfo.province}} {{addressInfo.city}} {{addressInfo.district}}
										{{addressInfo.detail}}
									</view>
								</view>
							</view>
							<view class="purchase_box_top_a_right" style="margin-left: 10rpx;">
								<u-icon name="arrow-right" size="24"></u-icon>
							</view>

						</view>
					</view>

					<view class="purchase_box_top_b">
						<view class="purchase_box_top_b_top">
							<view class="purchase_box_shang">
								<view style="display: flex;padding: 30rpx 0;width: 100%;" v-for="item in cartInfolist"
									:key="item.id">
									<view class="purchase_box_left">
										<image class="purchase_box_left_img" :src="item.productInfo.image"></image>
									</view>
									<view class="purchase_box_right">
										<text class="text1">{{item.productInfo.storeName}}</text>
										<view class="zhong">
											<text class="text2">{{item.productInfo.storeInfo}}</text>
											<!-- <van-icon name="arrow" size="12" color="#999999" /> -->
										</view>
										<view class="xia">
											<view class="xia_left">
												<text class="text3">￥
													<text class="text4">{{item.truePrice}}</text>
												</text>
											</view>
											<view class="xia_right">
												<view class="text5">
													<u-icon name="minus" size="16" color="#fff" @click="jian(item)">
													</u-icon>
												</view>
												<text class="text6">{{item.cartNum}}</text>
												<view class="text5">
													<u-icon name="plus" size="16" color="#fff" @click="jia(item)">
													</u-icon>
												</view>
											</view>
										</view>
									</view>
								</view>

							</view>
							<!-- <view class="peisong">
								<view class="peisong_left">
									商品金额
								</view>
								<view class="peisong_right">
									{{objpurchase.price}}
								</view>
							</view> -->
							<view class="peisong">
								<view class="peisong_left ">
									配送方式
								</view>
								<view class="peisong_right text">
									快递 免邮
								</view>
							</view>

						</view>
					</view>
					<view class="purchase_zhifu_fangshi">
						<view class="purchase_zhifu_fangshi_a">
							<view class="purchase_zhifu_top">支付方式</view>
							<view class="purchase_zhifu_bottom">
								<view
									style="display: flex;justify-content: space-between;align-items: center;padding: 26rpx 0px;">


									<radio-group @change="radioChange" class="radio-group">

										<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in list"
											:key="item.value">


											<view class="left" v-if="item.value == 0">
												<image class="img" :src="item.src"></image>
												{{item.name}}
												<text style="margin-left: 50rpx;color: #FF8F1F;">￥{{balance}}</text>
											</view>
											<view class="left" v-else>
												<image class="img" :src="item.src"></image>
												{{item.name}}

											</view>
											<view>
												<radio :value="String(item.value)" color='#00DF25'
													:checked="index == current" style="transform:scale(0.9)" />
											</view>
										</label>
										<view class="xian">

										</view>
									</radio-group>

								</view>

							</view>

						</view>
					</view>
					<view class="purchase_bottom">
						<view class="purchase_bottom_left">
							<text class="text1">合计：</text>
							<text class="text2">￥
								<text class="text3">{{orderPrice.payPrice}}</text></text>
						</view>
						<view class="purchase_bottom_right" @click="payment">
							立即支付
						</view>

					</view>


				</view>

			</view>
		</view>


	</view>
</template>
<script>
	import homeServices from '../../service/homeService'
	import shopping from '../../pages/shoppingmall/shopping.vue'
	export default {
		data() {
			return {
				obj: false,
				obj1: true,
				checked: false,
				// succesDialogshow: false,

				right: '',
				padding: '',
				top: '',
				height: '',
				num: 1,
				list: [{
						value: 0,
						name: '账户余额',
						src: '../../static/imgs/yue.png'
					},
					{
						value: 1,
						name: '微信支付',
						src: '../../static/imgs/weixin.png'
					}
				],
				li: {},
				ids: '',
				objpurchase: {},
				number: 0,
				cartId: '',
				addressInfo: {},
				display: true,



				checked: true,
				obj: true,
				ID: 1,
				right: '',
				padding: '',
				top: '',
				height: '',
				current: 0,
				lists: [],
				listss: [],
				listsss: [],
				selected: [],
				limit: 10,
				page: 1,
				ids: '',
				orderKey: '',
				cartInfolist: [],
				orderPrice: {
					payPrice: '计算中'
				},
				addressId: '0',
				balance: ''

			}
		},
		onShow() {

			// this.getpurchase()
			// if (!this.addressInfo.id) {
			// 	console.log(this.addressInfo.id)
			// 	console.log("么有地址信息   请求后台获取")
			// 	this.getCartInfo()
			// }



		},
		onLoad(options) {
			console.log(options, 'ddddddddddddddddd')
			this.accountinfo()
			this.getCartInfo()
			
		},
		methods: {
			//获取余额
			async accountinfo() {
				let res = await homeServices.accountinfo()
				this.balance = res.data.usableAmount
			},
			async getpurchase() {
				let id = uni.getStorageSync('iddd')
				// console.log(this.ids,llllllll)
				let res = await homeServices.commoditydetails(id)
				this.objpurchase = res.data.storeInfo

			},


			//获取购物·车订单数据
			async getCartInfo(data) {
				let params = {
					cartId: uni.getStorageSync('cartId'),
					addressId: uni.getStorageSync('addressId')
				}
				let res = await homeServices.orderConfirm(params)
				this.cartInfolist = res.data.cartInfo
				console.log(this.cartInfolist, 'zzzzzzzzzzzzz')
				this.orderKey = res.data.orderKey
				uni.setStorageSync('orderKey', res.data.orderKey)
				if (res.data.addressInfo) {
					this.display = false
					this.addressInfo = res.data.addressInfo
				}

				let paramss = {
					addressId: this.addressInfo.id,
					couponId: 0,
					shippingType: 1,
					useIntegral: 0,
					key: uni.getStorageSync('orderKey')
				}
				let res1 = await homeServices.getpostage(paramss)
				this.orderPrice.payPrice = res1.data.result.payPrice

			},

			shang() {
				uni.redirectTo({
					url: '/pages/shoppingmall/shopping'
				})
			},

			radioChange(evt) {
				for (let i = 0; i < this.list.length; i++) {
					if (this.list[i].value === evt.detail.value) {
						this.current = i;
						break;
					}
				}
				this.current = evt.detail.value

			},
			async jian(item) {
				console.log(item)
				let id = item.id
				if (item.cartNum > 1) {
					item.cartNum--
				} else {
					uni.showToast({
						title: '至少购买一件',
						icon: 'none'
					})
				}

				this.number = item.cartNum

				let params = {
					id: id,
					number: this.number
				}
				let res = await homeServices.cartnum(params)
				setTimeout(() => {
					// 模拟执行完毕
					// 改变 requestStatus
					this.getCartInfo()
				}, 1000);



			},
			async jia(item) {
				let id = item.id
				if (item.cartNum < item.trueStock) {
					item.cartNum++
				} else {
					uni.showToast({
						title: '没有多余商品了',
						icon: 'none'
					})
				}

				this.number = item.cartNum

				let params = {
					id: id,
					number: this.number
				}
				let res = await homeServices.cartnum(params)
				setTimeout(() => {

					this.getCartInfo()
				}, 1000);

			},
			payment() {
				this.createOrder()
			},
			 createOrder() {

				if (this.current == 0) {
					var paytype = 'yue'
					var payfrom = 'yue'
				} else {
					var paytype = 'weixin'
					var payfrom = 'app'
				}
				if (this.addressInfo.id == undefined) {
					uni.showToast({
						title: "请选择收货地址",
						icon: 'none'
					})
					return
				}
				
				
				uni.showModal({
					title: '提示',
					content: '确定是否支付',
					confirmText:'确定',
					cancelText:'取消',
					success: async res=>  {
						if (res.confirm) {
							console.log('用户点击确定');
							let paramss = {
								addressId: this.addressInfo.id,
								couponId: 0,
								shippingType: 1,
								useIntegral: 0,
								from: payfrom,
								key: uni.getStorageSync('orderKey'),
								payType: paytype
							}
							let res1 = await homeServices.createOrder(paramss)
							if (res1.status == 200) {
								// console.log(res1.msg);
								if (paytype == 'yue') {
									uni.showToast({
										title: res1.msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.redirectTo({
											url: '/pagesB/mallorders/mallorders?id=' + 1
										})
									}, 2000)
								} else {
									console.log(res1.data.result.jsConfig);
									let orderinfo = res1.data.result.jsConfig
									uni.requestPayment({
										provider: 'wxpay',
										orderInfo: orderinfo, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
										success: function(res) {
											uni.showModal({
												title: "提示",
												content: "支付成功",
												showCancel: false,
												confirmText: "确定",
												success: function(reb) {
													console.log(reb);
													if (reb.confirm) {
														uni.navigateBack({
							
														})
													}
												},
												fail: function(err) {
													console.log(err);
												}
											})
										},
										fail: function(err) {
											console.log('fail:' + JSON.stringify(err));
										}
									});
								}
							
							}else{
								uni.showToast({
									title: res1.msg,
									icon: 'none'
								})
							}
						} else if (res.cancel) {
							return
						}
					}
				
				})
				

			},
			chargeBtn(action, done) {
				console.log(action, done)

			},
			homepage() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			determined() {
				// this.succesDialogshowsss=true
				uni.redirectTo({
					url: '/pagesB/shippingadd/shippingadd?cartIds=' + this.cartId
				})
			},












		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/sass/purchase.scss'
</style>
